package com.shujia.base;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Code11ScanData {
    static Connection connection;

    static {
        // 创建连接需要给定连接信息，所以需要给定配置类
        Configuration conf = new Configuration();

        // 由于对HBASE进行操作时，需要连接其Zookeeper 那么需要传入Zookeeper的IP
        conf.set("hbase.zookeeper.quorum", "node1,node2,master");
        // 获取连接对象 通过ConnectionFactory来创建连接对象

        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {

        // 对于数据进行操作，可以直接获取 TABLE 对象来操作
        TableName tableName = TableName.valueOf("api:tb1");
        Table table = connection.getTable(tableName);

        Scan scan = new Scan();

        // 通过Scanner扫描器得到的结果是一个Result的集合类
        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {

            String name = Bytes.toString(result.getValue("info1".getBytes(StandardCharsets.UTF_8)
                    , "name".getBytes(StandardCharsets.UTF_8)));

            String age = Bytes.toString(result.getValue("info1".getBytes(StandardCharsets.UTF_8)
                    , "age".getBytes(StandardCharsets.UTF_8)));

            String gender = Bytes.toString(result.getValue("info1".getBytes(StandardCharsets.UTF_8)
                    , "gender".getBytes(StandardCharsets.UTF_8)));

            String clazz = Bytes.toString(result.getValue("info2".getBytes(StandardCharsets.UTF_8)
                    , "clazz".getBytes(StandardCharsets.UTF_8)));

            System.out.println("name:"+name+" age:"+age+" gender:"+gender+" clazz:"+clazz);

        }


    }
}
